package com.project.dao.impl;

import com.project.bean.CountryBean;
import com.project.dao.ICountryDao;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class CountryDaoImpl extends BaseDao implements ICountryDao {
    @Override
    public List<CountryBean> findAll() {
        List<CountryBean> list =new ArrayList<>();
        this.setConnection();
        try {
            ps=con.prepareStatement("select * from t_country");
            rs=ps.executeQuery();
            while (rs.next()){
                CountryBean country = this.getCountry(rs);
                list.add(country);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    private CountryBean getCountry(ResultSet rs) throws SQLException {
        CountryBean country = new CountryBean();
        country.setId(rs.getInt("pk_countryId"));
        country.setName(rs.getString("c_name"));
        return country;
    }
}
